package com.firefly.service.query.cms;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.firefly.dao.dao.cms.mapper.LinkMapper;
import com.firefly.dao.dao.cms.po.LinkPo;
import com.firefly.dao.dao.cms.service.LinkService;
import com.firefly.dao.assembler.cms.LinkAssembler;
import com.firefly.dao.dto.CommonDto;
import com.firefly.dao.dto.cms.LinkSearchDto;
import com.firefly.dao.vo.CommonListVo;
import com.firefly.dao.vo.cms.LinkVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @description: 链接
 * @author: lzg(萤火科技 ： 854378082 @ qq.com)
 * @date: 2022/12/20
 **/
@Service
public class LinkQuery {
    private final LinkAssembler linkAssembler;
    private final LinkService linkService;
    private final LinkMapper linkMapper;
    @Autowired
    public LinkQuery(LinkAssembler linkAssembler, LinkService linkService, LinkMapper linkMapper){
        this.linkAssembler = linkAssembler;
        this.linkService = linkService;
        this.linkMapper = linkMapper;
    }
    // 获取列表
    public CommonListVo getList(LinkSearchDto request) {
        QueryWrapper<LinkPo> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(request.key != "", "title", request.key).eq("del_flag",0);
        Long count = linkService.count(queryWrapper);
        queryWrapper.last("limit " + request.offset + "," + request.pageSize).orderByDesc("sort","id");
        List<LinkVo> list = linkMapper.getListAndTypeName(request);
        CommonListVo result = new CommonListVo();
        result.setList(list);
        result.setCount(count);
        return result;
    }
    public LinkVo getDetail(CommonDto request){
        return linkAssembler.getVo(linkService.getById(request.detailId));
    }
}
